package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.AffairsMapper;
import com.village.zpark.pojo.Affairs;
import com.village.zpark.service.AffairsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class AffairsServiceImpl implements AffairsService {

    @Autowired
    private AffairsMapper affairsMapper;
    @Override
    public List<Affairs> findAll() {
        return affairsMapper.selectAll();
    }

    @Override
    public Affairs findById(Integer id) {
        return affairsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addAffairs(Affairs affairs) {
        return affairsMapper.insert(affairs);
    }

    @Override
    public int updateAffairs(Affairs affairs) {
        return affairsMapper.updateByPrimaryKey(affairs);
    }

    @Override
    public int deleteAffairsById(Integer id) {
        return affairsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Affairs> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Affairs.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("titles", "%" + param + "%"); // 使用 orLike 来实现多字段模糊查询
        }
        List<Affairs> affairs;
        if (param != null && !param.isEmpty()) {
            affairs = affairsMapper.selectByExample(example);
        } else {
            affairs = affairsMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(affairs);
    }
}
